import { Module } from 'vuex'
import { getUserInfo } from '@/api/loginApi'

const user: Module<any, any> = {
    state: {
        userInfo: "",
        roles: [],
        menus: []
    },
    mutations: {
        SET_USERINFO(state, userInfo) {
            state.userInfo = userInfo
        },
        SET_ROLES(state, ROLES) {
            state.roles = ROLES
        },
        SET_MENUS(state, MENUS) {
            state.menus = MENUS
        }
    },
    actions: {
        GET_USERINFO({ commit }) {
            return new Promise((resolve, reject) => {
                getUserInfo().then((resd: any) => {
                    commit('SET_USERINFO', resd)
                    commit('SET_ROLES', resd.roleCodeStr.split(","))
                    commit('SET_MENUS', resd.menus)
                    resolve({})
                })
            })
        },
        REMOVE_USERINFO({ commit }) {
            commit('SET_USERINFO', "")
        }
    },

}

export default user